﻿using UnityEngine;

public class DrawLine : MonoBehaviour
{
    private LineRenderer lineRenderer;
    public GameObject source;
    public GameObject target;
    private BrainCore srcCore;
    private BrainCore tarCore;
    private float rate;

    public float Rate
    {
        get { return rate; }
        set { rate = value; lineRenderer.SetWidth(rate, rate); }
    }

    void Start()
    {
        srcCore = source.GetComponent<BrainCore>();
        tarCore = target.GetComponent<BrainCore>();

        lineRenderer = (LineRenderer)gameObject.GetComponent("LineRenderer");
        lineRenderer.useWorldSpace = true;
        //lineRenderer.enabled = false;
        Rate = 0.2f;
    }

    void Update()
    {
        if (source == null || target == null)
        {
            Destroy(gameObject);
            return;
        }
        lineRenderer.SetPosition(0, source.transform.position);
        lineRenderer.SetPosition(1, target.transform.position);

        float outnum = srcCore.threshold;
        if (outnum != 0)
        {
            tarCore.addValue(outnum);
            Color color = outnum > 0 ? Color.white : Color.black;
            lineRenderer.SetColors(color, color);
        }
        else
        {
            lineRenderer.SetColors(Color.gray, Color.gray);
        }
    }

    
}